package com.chwn.curiousdiary.utils;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chwn.curiousdiary.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by Myshikin on 2017-03-22.
 * desc:图片加载统一使用Glide
 */

public class ImgLoadUtil {

    private static ImgLoadUtil instance;

    private ImgLoadUtil() {
    }

    public static ImgLoadUtil getInstance() {
        if (instance == null) {
            instance = new ImgLoadUtil();
        }
        return instance;
    }

    //--------------------------------------

    /**
     * 加载本地图片
     * @param imageView
     * @param id
     */
    public static void loadLocalImg(ImageView imageView, int id) {
        Glide.with(imageView.getContext())
                .load(id)
                .into(imageView);

    }


    /**
     * 将gif图转换为静态图
     */
    public static void displayGif(String url, ImageView imageView) {

        Glide.with(imageView.getContext()).load(url)
                .asBitmap()
                .placeholder(R.drawable.img_one_bi_one)
                .error(R.drawable.img_one_bi_one)
                //                .skipMemoryCache(true) //跳过内存缓存
                //                .crossFade(1000)
                //                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// 缓存图片源文件（解决加载gif内存溢出问题）
                //                .into(new GlideDrawableImageViewTarget(imageView, 1));
                .into(imageView);
    }

    /**
     * 音频，视频
     * 默认图区别
     */
    public static void displayEspImage(String url, ImageView imageView, int type) {
        Glide.with(imageView.getContext())
                .load(url)
                .crossFade(500)
                .placeholder(getDefaultPic(type))
                .error(getDefaultPic(type))
                .into(imageView);
    }

    private static int getDefaultPic(int type) {
        switch (type) {
            case 0:// 音频
                return R.drawable.icon_story_placeholder;
            case 1:// 视频
                return R.drawable.icon_story_placeholder;

        }
        return R.drawable.icon_story_placeholder;
    }

    /**
     * 显示高斯模糊效果（音频播放背景）
     */
    private static void displayGaussian(Context context, String url, ImageView imageView) {
        // "23":模糊度；"4":图片缩放4倍后再进行模糊
        Glide.with(context)
                .load(url)
                .error(R.drawable.stackblur_default)
                .placeholder(R.drawable.stackblur_default)
                .crossFade(500)
                .bitmapTransform(new BlurTransformation(context, 23, 4))
                .into(imageView);

        Glide.with(context)
                .fromBytes()
                .error(R.drawable.stackblur_default)
                .placeholder(R.drawable.stackblur_default)
                .crossFade(500)
                .bitmapTransform(new BlurTransformation(context, 23, 4))
                .into(imageView);
    }

    /**
     * 加载圆角图
     */
    public static void displayCircle(ImageView imageView, String imageUrl) {
        displayCircle(imageView, imageUrl, getDefaultPic(0));
    }

    public static void disPlayStoryType(ImageView imageView, int id) {
        Glide.with(imageView.getContext())
                .load(id)
                .into(imageView);
    }

    /**
     * 故事列表图片加载
     * @param imageView
     * @param imageUrl
     */
    public static void displayStoryImage(ImageView imageView, String imageUrl) {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .crossFade(500)
                .centerCrop()
                .placeholder(R.drawable.icon_one_to_one)
                .error(R.drawable.icon_one_to_one)
                .into(imageView);
    }


    /**
     * 加载圆角图
     */
    public static void displayCircle(ImageView imageView, String imageUrl, int id) {
        Glide.with(imageView.getContext().getApplicationContext())
                .load(Uri.parse(imageUrl))
                .placeholder(id)
                .crossFade(1000)
                .transform(new GlideCircleTransform(imageView.getContext()))
                .into(imageView);
    }

    /**
     * 音频，视频列表图
     * @param defaultPicType
     *         音频：0 ; 视频：1 ;
     */
    public static void displayFadeImage(ImageView imageView, String url, int defaultPicType) {
        displayEspImage(url, imageView, defaultPicType);
    }

    /**
     * 视频列表图片
     */
    public static void showVideoImg(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .crossFade(500)
                .override((int) CommonUtils.getDimens(R.dimen.video_detail_width), (int) CommonUtils.getDimens(R.dimen.video_detail_height))
                .placeholder(getDefaultPic(0))
                .error(getDefaultPic(0))
                .into(imageView);
    }

    /**
     * 音频列表图片
     */
    public static void showAudiomg(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .crossFade(500)
                .override((int) CommonUtils.getDimens(R.dimen.audio_detail_width), (int) CommonUtils.getDimens(R.dimen.audio_detail_height))
                .placeholder(getDefaultPic(2))
                .error(getDefaultPic(2))
                .into(imageView);
    }

    public static void showBannermg(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .centerCrop()
                .error(getDefaultPic(2))
                .into(imageView);
    }

    /**
     * 音频播放页显示高斯背景图
     */
    public static void showImgBg(ImageView imageView, String url) {
        displayGaussian(imageView.getContext(), url, imageView);
    }
}
